package com.example.backend.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.backend.common.Result;
import com.example.backend.entity.Admin;
import com.example.backend.mapper.AdminMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminMapper adminMapper;

    @PostMapping("/register")
    public Result<?> register(@RequestBody Admin admin){
        adminMapper.insert(admin);
        return Result.success();
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody Admin admin){
        Admin res = adminMapper.selectOne(Wrappers.<Admin>lambdaQuery().eq(Admin::getId,admin.getId()).eq(Admin::getPassword,admin.getPassword()));
        if (res == null){
            return Result.error("-1","用户名或密码错误");
        }
//        String token = TokenUtils.genToken(res);
//        res.setToken(token);
        res.setPassword("");    //不传密码
        return Result.success(res);
    }
}
